// pages/publishTask/index.js
const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    file_name:'',
    fileID:'',
    file_type:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo'),openid=wx.getStorageSync('OPENID')
    this.setData({
      openid:openid,
      userInfo:userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  addFile: function(){
    wx.chooseMessageFile({
      count: 1,
      type:'all',
      success: (res)=>{
        console.log(res)
        let tempFile = res.tempFiles[0]
        this.setData({
          file_name: util.subStrFormat(tempFile.name,10),
          file_type:tempFile.type
        })
        this.uploadFile(tempFile.name,tempFile.path)
      }
    })
  },
    //上传文件
    uploadFile(fileName,tempFile){
      wx.cloud.uploadFile({
        cloudPath:'file/'+fileName,
        filePath:tempFile,
      })
    .then(res=>{
      console.log("文件上传成功",res);
      this.setData({
        fileID:res.fileID
      })
    })
    .catch(err=>{
      console.log("文件上传失败",err);
    })
    },
  showFile:function(){
      //下载文件
      wx.cloud.downloadFile({
        fileID: this.data.fileID,
        success: res => { 
          console.log("文件下载成功",res);
          //打开文件
          const filePath = res.tempFilePath
          if(this.data.file_type=='image'){
            wx.previewImage({
              urls: [filePath],
              success: (res) => {
                console.log('文件打开成功',res)
              },
            })
          }else{
          wx.openDocument({
            filePath: filePath,
            success: (res) => {
              console.log('文件打开成功',res)
            },
            fail:(res)=>{
              wx.showToast({
                title: '文件格式错误！',
              })
              console.error(res)
            }
          })
          }

        }
    })
  },
  deletefile:function(){
    this.setData({
      file_name:'',
      fileID:''
    })
  },
  formSubmit: function (e) {
    let data = e.detail.value
    let datavalid = this.checkdata(data)
    if (!datavalid) {
      return
    }
    Object.assign(
      data,
      { nickname: this.data.userInfo.nickname || `用户${this.data.openid.slice(20)}` },
      {fileID: this.data.fileID},
      {fileType: this.data.file_type},
      { date: Date.now() },
      { status: false }
    )
    wx.cloud.database().collection('tasklist').add({
      data: data,
      success: (res) => {
        console.log(res)
        wx.showToast({
          title: '发布成功',
          success: (res)=>{
            setTimeout(() => {
              wx.navigateBack({
              delta: 1,
            })
            }, 2000);
            
          }
        })
      },
      fail:(res)=>{
      }
    })
  },
  formReset: function () {
    this.setData({
    })
  },
  checkdata(data) {
    console.log(data)
    if (
      !data.title ||
      !data.wxid ||
      !data.money ||
      !data.lastday||
      !data.lasttime||
      !data.label.length
    ) {
      wx.showToast({
        title: '发生错误：信息不完整！',
        icon: 'none',
        image: '',
        duration: 2000,
        mask: false,
        success: (result) => {},
        fail: () => {},
        complete: () => {},
      })
      return false
    }else{
      return true
    }
  },
  bindDateChange(e){
    this.setData({
      lastday: e.detail.value,
    })
  },
  bindTimeChange(e){
    this.setData({
      lasttime: e.detail.value,
    })
  }
})